package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.Adapter.LogisticsAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.LogisticstBean;
import com.mmlc.meimei.lichengkongjian.been.SendTraceBean;
import com.mmlc.network.filter.SendTraceRequestFilter;
import com.mmlc.network.responsebean.SendTraceResponseBean;
import com.mmlc.utils.ToastUtils;
import com.mmlc.widget.TitleBarView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class LogisticsListActivity extends InterActivity implements OnClickListener {


    @InjectView(R.id.tb_titleBar)
    TitleBarView tbTitleBar;
    @InjectView(R.id.lv_shopList)
    ListView lvShopList;
    @InjectView(R.id.rootView)
    RelativeLayout rootView;
    private String orderId;

    @Override
    protected int initContentView() {
        return R.layout.activity_logistics;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);

        processData();
    }

    private void processData() {

        tbTitleBar.ll_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finishUI(false);
            }
        });
        tbTitleBar.setCenterName("查看物流");

        Intent intent = getIntent();
        orderId = intent.getStringExtra("orderId");
        
        initListView();
        requestNet();

    }

    private void initListView() {
        lvShopList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(filter.sendTraceList.size()==0){
                    return;
                }


                SendTraceBean traceBean = filter.sendTraceList.get(position);

                jmpToTraceUI(traceBean);

            }
        });
    }

    private void jmpToTraceUI(SendTraceBean traceBean) {
        Intent intent = new Intent(LogisticsListActivity.this,ActivityShopSendTrace.class);
        intent.putExtra("traceBean",traceBean);
        startActivity(intent);
    }

    SendTraceRequestFilter filter;
    private void requestNet() {
        filter = new SendTraceRequestFilter(this);
        filter.requestBean.order_id = this.orderId;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<SendTraceResponseBean>() {
            @Override
            public void onSuccess(SendTraceResponseBean result) {
                adapterItemList = filter.responseBean;

                if(adapterItemList.size() > 0){
                    processAdapter();
                }else{
                    filter.AddNoDataUI();
                }

            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }

    LogisticsAdapter adapter;
    List<LogisticstBean> adapterItemList = new ArrayList<>();
    private void processAdapter() {
        if(adapter == null){
            adapter = new LogisticsAdapter(adapterItemList);
            lvShopList.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }

    }

    /**
     * true:刷新
     * false: 不刷新
     */
    public void finishUI(boolean isRefresh) {
        if (isRefresh) {//通知接收者，要刷新界面
            Intent intent = new Intent();
            intent.putExtra("state", 1);
            setResult(RESULT_OK, intent);
        }
        finish();
    }

}
